package com.practice.xml.jdom;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

import com.practice.xml.Company;
import com.practice.xml.CompanyDefinition;
import com.practice.xml.Employee;
import com.practice.xml.EmployeeDefinition;
import com.practice.xml.XmlProcessor;

/**
 * Creates and parses XML document by JDOM.
 * @author Leonardo
 *
 */
public class JdomXmlProcessor implements XmlProcessor {

	@Override
	public void createXml(Company company, String filePath) {
		Document document;
		Element companyElement = new Element(CompanyDefinition.NAME);
		document = new Document(companyElement);
		
		Attribute nameAttribute = new Attribute(CompanyDefinition.ATTRIBUTE_NAME, company.getName());
		companyElement.setAttribute(nameAttribute);
		
		Element employeesElement = new Element(CompanyDefinition.ELEMENT_EMPLOYEES);
		companyElement.addContent(employeesElement);
		
		for (Employee employee : company.getEmployees()) {
			Element employeeElement = new Element(EmployeeDefinition.NAME);
			
			Element nameElement = new Element(EmployeeDefinition.ELEMENT_NAME);
			nameElement.setText(employee.getName());
			employeeElement.addContent(nameElement);
			
			Element sexElement = new Element(EmployeeDefinition.ELEMENT_SEX);
			sexElement.setText(Boolean.toString(employee.getSex()));
			employeeElement.addContent(sexElement);
			
			Element ageElement = new Element(EmployeeDefinition.ELEMENT_AGE);
			ageElement.setText(Integer.toString(employee.getAge()));
			employeeElement.addContent(ageElement);
			
			employeesElement.addContent(employeeElement);
		}
		
		XMLOutputter xmlOutputter = new XMLOutputter();
		try {
			xmlOutputter.output(document, new FileOutputStream(filePath));
		} catch (FileNotFoundException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		} catch (IOException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		}
	}

	@Override
	public Company parseXml(String filePath) {
		@SuppressWarnings("deprecation")
		SAXBuilder saxBuilder = new SAXBuilder(false);
		
		Document document = null;
		try {
			document = saxBuilder.build(new FileInputStream(filePath));
		} catch (FileNotFoundException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		} catch (JDOMException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		} catch (IOException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		}
		
		Company company = new Company();
		Element companyElement = document.getRootElement();
		Attribute nameAttribute = companyElement.getAttribute(CompanyDefinition.ATTRIBUTE_NAME);
		company.setName(nameAttribute.getValue());
		
		Element employeesElement = companyElement.getChild(CompanyDefinition.ELEMENT_EMPLOYEES);
		List<Employee> employees = new ArrayList<>();
		company.setEmployees(employees);
		
		for (Element employeeElement : employeesElement.getChildren(EmployeeDefinition.NAME)) {
			Employee employee = new Employee();
			
			Element nameELement = employeeElement.getChild(EmployeeDefinition.ELEMENT_NAME);
			employee.setName(nameELement.getText());
			
			Element sexElement = employeeElement.getChild(EmployeeDefinition.ELEMENT_SEX);
			employee.setSex(Boolean.parseBoolean(sexElement.getText()));
			
			Element ageElement = employeeElement.getChild(EmployeeDefinition.ELEMENT_AGE);
			employee.setAge(Integer.parseInt(ageElement.getText()));
			
			employees.add(employee);
		}
		
		return company;
	}
	
}
